<?php 
class archin_Model_Service 
{
	public function __construct()
	{
		$this->db = Zend_Db_Table::getDefaultAdapter();
	}
	
	public function find($node, $id)
	{
		$model = "archin_Model_" . ucwords($node);
		$m = new $model();
		return $m->findByPrimary($id);
	}

	public function insertNewPeople($data)
	{
		$mPeople = new archin_Model_People();
		//prepare data
		$keys = array ("name_vi", "description_vi", "description_en");
		$dataToInsert = clu_array_merge($keys, $data);
		$dataToInsert['name_en'] = VN_unaccent($dataToInsert['name_vi']);
		try 
		{
			$mPeople->insert($dataToInsert);
			$id = $this->db->lastInsertId();
			if (isset($_FILES['avatar']))
			{
				$uploadedDir = APP_PATH . "/public/archin/people/$id/";
				if (!file_exists($uploadedDir))
                                mkdir($uploadedDir, 0777  , true);
				move_uploaded_file($_FILES['avatar']['tmp_name'], $uploadedDir . "avatar.jpg");
			}
			$ret['success'] = true;			
			$ret['insertedId'] = $id;
		}
		catch (Exception $e)
		{
			$ret['success'] = false;
			$ret['errorMessage'] = $e->getMessage();
		}
		return $ret;
	}
	

	public function updatePeople($id, $data)
	{
		
		$mPeople = $this->find("people", $id);
		//prepare data
		$keys = array ("name_vi", "description_vi", "description_en");
		$dataToInsert = clu_array_merge($keys, $data);
		$dataToInsert['name_en'] = VN_unaccent($dataToInsert['name_vi']);
		try 
		{
			foreach ($dataToInsert as $key=>$val)
				$mPeople[$key] = $val; 
			$mPeople->save();
			
			if (isset($_FILES['avatar']))
			{
				$uploadedDir = APP_PATH . "/public/archin/people/$id/";
				if (!file_exists($uploadedDir))
                                mkdir($uploadedDir, 0777, true);
				move_uploaded_file($_FILES['avatar']['tmp_name'], $uploadedDir . "avatar.jpg");
			}
			$ret['success'] = true;			
		}
		catch (Exception $e)
		{
			$ret['success'] = false;
			$ret['errorMessage'] = $e->getMessage();
		}
		return $ret;
	}
	
	public function insertNewProject($data)
	{
		$m = new archin_Model_Project();
		//prepare data
		$keys = array ("category", "name_vi", "name_en", "description_vi", "description_en");
		$dataToInsert = clu_array_merge($keys, $data);
		try 
		{
			$m->insert($dataToInsert);
			$ret['success'] = true;			
			$ret['insertedId'] = $this->db->lastInsertId();
		}
		catch (Exception $e)
		{
			$ret['success'] = false;
			$ret['errorMessage'] = $e->getMessage();
		}
		return $ret;
	}
	

	public function updateProject($id, $data)
	{
		$m = $this->find("project", $id);
		//prepare data
		$keys = array ("category", "name_vi", "name_en", "description_vi", "description_en");
		$dataToInsert = clu_array_merge($keys, $data);
//		die_a($dataToInsert);
		try 
		{
			foreach ($dataToInsert as $key=>$val)
				$m[$key] = $val; 
			$m->save();
			$ret['success'] = true;			
		}
		catch (Exception $e)
		{
			$ret['success'] = false;
			$ret['errorMessage'] = $e->getMessage();
		}
		return $ret;
	}	
	
	
	
	public function insertNewPost($data)
	{
		$m = new archin_Model_Post();
		//prepare data
		$keys = array ("page", "area", "code", "name_vi", "description_vi", "description_en");
		$dataToInsert = clu_array_merge($keys, $data);
		try 
		{
			$m->insert($dataToInsert);
			$ret['success'] = true;			
			$ret['insertedId'] = $this->db->lastInsertId();
		}
		catch (Exception $e)
		{
			$ret['success'] = false;
			$ret['errorMessage'] = $e->getMessage();
		}
		return $ret;
	}
	

	public function updatePost($id, $data)
	{
		$m = $this->find("post", $id);
		//prepare data
		$keys = array ("page", "area", "code", "name_vi", "description_vi", "description_en");
		$dataToInsert = clu_array_merge($keys, $data);
		//		die_a($dataToInsert);
		try 
		{
			foreach ($dataToInsert as $key=>$val)
				$m[$key] = $val; 
			$m->save();
			$ret['success'] = true;			
		}
		catch (Exception $e)
		{
			$ret['success'] = false;
			$ret['errorMessage'] = $e->getMessage();
		}
		return $ret;
	}	
}